package com.fudong.floatwindows;

import android.content.Context;
import android.graphics.Canvas;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.Nullable;

public class HorizontalTextView extends BaseCustomTextView {

    public HorizontalTextView(Context context) {
        super(context);
    }

    public HorizontalTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HorizontalTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public HorizontalTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(TextUtils.isEmpty(text)){
            return;
        }
        canvas.save();
        super.onDraw(canvas);
        if(staticLayout == null){
            staticLayout = new StaticLayout(text, textPaint, canvas.getHeight() - getPaddingLeft() - getPaddingRight(), Layout.Alignment.ALIGN_NORMAL, 1.0f,0.0f , false);
        }
        if(mChangedemotion < - (getWidth() - getPaddingTop())){
            //到达底部
            mChangedemotion = staticLayout.getHeight() + getPaddingTop();
            return;
        }

        if(mChangedemotion > getPaddingTop() + staticLayout.getHeight()){
            //到达底部
            mChangedemotion -= (getWidth() + staticLayout.getHeight());
            return;
        }
        canvas.rotate(90, getMeasuredWidth() / 2, getMeasuredHeight() / 2);
        canvas.translate(- (canvas.getHeight() - canvas.getWidth())/2 + getPaddingLeft(),(canvas.getHeight() - canvas.getWidth())/2 + getPaddingTop() - mChangedemotion);
        staticLayout.draw(canvas);
    }

    //触摸事件
    private float mStartX;
    private float mStartY;
    private float mChangedemotion;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        handler.removeCallbacksAndMessages(null);
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mStartX = event.getRawX();
                mStartY = event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                float newX = event.getRawX();
                float newY = event.getRawY();

                //偏移量
                float dx = newX - mStartX;
                //移动
                mChangedemotion += dx;

                //从新赋值
                mStartX = newX;
                mStartY = newY;
                break;
            case MotionEvent.ACTION_UP:
                if(isAutoScroll){
                    handler.postDelayed(this,delayed);
                }
                break;
        }
        //重绘--->区别于从新布局requestLayout();
        invalidate();
        return true;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public int dip2px(float dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    @Override
    public void run() {
        mChangedemotion += scrollSpeed;
        invalidate();
        handler.postDelayed(this,delayed);
    }
}
